package cc.sunni;

import com.google.common.base.Functions;
import com.google.common.collect.Collections2;

import java.math.BigDecimal;
import java.util.function.*;

/**
 * @author jiangli
 * @since 2021/1/7 9:43
 */
public class LambdaTest {

    public static void main(String[] args) {
        // Predicate Boolean demo.test(T t) 判断真假. filter内部就是Predicate接口
        Predicate<Integer> predicate = x -> x > 185;
        Student student = new Student("xiaomi", 23, 175);
        System.out.println("xiaomi的身高高于185吗？：" + predicate.test(student.getStature()));

        // Consumer accept(T t) 消费消息,返回void
        Consumer<String> consumer = System.out::println;
        consumer.accept("命运由我不由天");

        // Function R apply(T t) 将T映射为R(转换). map内部就是Function接口
        Function<Student, String> function = Student::getName;
        String name = function.apply(student);
        System.out.println(name);

        // Supplier T get() 生产消息
        Supplier<Integer> supplier =
                () -> Integer.valueOf(BigDecimal.TEN.toString());
        System.out.println(supplier.get());

        // UnaryOperator T apply(T t) 一元操作
        UnaryOperator<Boolean> unaryOperator = x -> !x;
        Boolean apply2 = unaryOperator.apply(true);
        System.out.println(apply2);

        // BinaryOperator T apply(T t,T u) 二元操作
        BinaryOperator<Integer> operator = (x, y) -> x * y;
        Integer integer = operator.apply(2, 3);
        System.out.println(integer);

        test(() -> "我是一个演示的函数式接口");
    }

    /**
     * 演示自定义函数式接口使用
     */
    public static void test(Worker worker) {
        String work = worker.work();
        System.out.println(work);
    }

    public interface Worker {
        String work();
    }

    static class Student {
        private String name;

        private Integer age;

        private Integer stature;

        public Student(String name, Integer age, Integer stature) {
            this.name = name;
            this.age = age;
            this.stature = stature;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public Integer getStature() {
            return stature;
        }

        public void setStature(Integer stature) {
            this.stature = stature;
        }
    }

}
